package com.stlm2.dems.service;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.dao.DdmGoodsDao;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.DdmGoods;
import com.stlm2.dems.entity.User;
import com.stlm2.util.StringUtils;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.FootMark;
import com.stlm2.dems.dao.FootMarkDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("footMarkService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class FootMarkService extends BaseServiceImpl<FootMark> {

	@Resource
	private FootMarkDao footMarkDao;
	@Resource
	private UserDao userDao;
	@Resource
	private DdmGoodsDao ddmGoodsDao;
	@Override
	protected BaseDao <FootMark> getDao() {
		return footMarkDao;
	}


	/**
	 * 添加我的足迹
	 * ffl
	 * 2017-10-17 09:49:40
	 * @param token
	 * @param goodsId
	 * @return
	 */
	public String addFootMark(String token,String goodsId){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if(!NumberUtils.isDigits(goodsId)){
			return CodeContent.getModelByCode(0,"请添加浏览过的商品",null);
		}
		List<FootMark> old=footMarkDao.getByGoodsId(user.getId(),NumberUtils.toInt(goodsId));
		int num=1;
		if (old!=null&&old.size()>0){//该用户已浏览过该商品
			if(old.size()==1){
				FootMark footMark=old.get(0);
				num+=footMark.getNum();
				footMark.setNum(num);
				footMark.setState(true);
				footMark.setModifyTime(new Date());
				footMarkDao.modify(footMark);
			}else {//数据错误，该商品记录被重复添加， 在此修正
				Date firstTime=new Date();
				for (FootMark footMark:old) {
					num+=footMark.getNum();
					firstTime=footMark.getCreateTime();
					footMarkDao.delete(footMark.getId());
				}
				FootMark footMark=new FootMark();
				footMark.setUserId(user.getId());
				footMark.setGoodsId(NumberUtils.toInt(goodsId));
				footMark.setCreateTime(firstTime);
				footMark.setState(true);
				footMark.setNum(null);
				footMark.setModifyTime(new Date());
				footMarkDao.add(footMark);
			}
		}else {//第一次浏览该商品
			FootMark footMark=new FootMark();
			footMark.setUserId(user.getId());
			footMark.setGoodsId(NumberUtils.toInt(goodsId));
			footMark.setCreateTime(new Date());
			footMark.setState(true);
			footMark.setNum(num);
			footMark.setModifyTime(new Date());
			footMarkDao.add(footMark);
		}


		return CodeContent.getModelByCode(1,"",null);
	}



	/**
	 * 我的足迹
	 * ffl
	 * 2017-10-17 11:00:44
	 * @param token
	 * @param pageNu
	 * @return
	 */
	public String myFootMark(String token,String pageNu){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		int pageNo=1;
		if (NumberUtils.isDigits(pageNu)){
			pageNo=NumberUtils.toInt(pageNu);
		}else {
			return CodeContent.getModelByCode(0,"指定查询页数",null);
		}
		FootMark param=new FootMark();
		param.setUserId(user.getId());
		param.setState(true);//前台显示 状态为 true 的记录
		param.setModifyTime(new Date());// 按最新浏览时间降序查询
		PageView pageView=footMarkDao.queryPageView(pageNo,10,param);
		ModelMap model=new ModelMap();
		if (pageView!=null){
			model.put("footMarkList",convert(pageView.getRecords()));
			model.put("recordsTotal", pageView.getRowCount());
			model.put("totalPage", pageView.getPageCount());
		}
		model.put("pageNo", pageNo);
		return CodeContent.getModelByCode(1,"",model);
	}

	public List<ModelMap> convert(List<FootMark> footMarkList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (footMarkList != null && footMarkList.size() > 0) {
			for (FootMark footMark : footMarkList) {
				ModelMap map = buildModel(footMark);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel(FootMark footMark) {
		ModelMap model = new ModelMap();
		if(footMark!=null) {
			DdmGoods goods=ddmGoodsDao.getById(footMark.getGoodsId());
			if(goods!=null) {
				model.put("id", footMark.getId());
				model.put("goodsId", goods.getId());
				model.put("name", goods.getName());
				model.put("salesPrice", goods.getMemberPrice());
				model.put("goodsState", goods.getState());
				model.put("mainImage", goods.getMainImg());
			}
		}
		return  model;
	}


	/**
	 * 删除我的足迹
	 * ffl
	 * 2017-10-17 11:05:30
	 * @param token
	 * @param idStr
	 * @return
	 */
	public String deleteById(String token,String idStr){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if(StringUtils.isNullOrEmpty(idStr)){
			return CodeContent.getModelByCode(0,"请指定要删除浏览过的商品记录",null);
		}
		String[] id=idStr.split(":");
		for (int i=0;i<id.length;i++){
			FootMark footMark=footMarkDao.getById(NumberUtils.toInt(id[i]));
			if(footMark!=null){
				if(footMark.getUserId()-user.getId()==0){
					footMark.setState(false);
					footMarkDao.modify(footMark);
				}else {
					return CodeContent.getModelByCode(0,"要删除的浏览记录不是您的浏览记录",null);
				}
			}
		}

		return CodeContent.getModelByCode(1,"",null);
	}


	/**
	 * 清空我的足迹
	 * ffl
	 * 2017-10-17 11:12:51
	 * @param token
	 * @return
	 */
	public  String cleanFootMark(String token){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		footMarkDao.updateByUserId(user.getId());
		return CodeContent.getModelByCode(1,"",null);
	}


	/**
	 * 我的足迹 记录 个数
	 * ffl
	 * 2017-10-17 11:20:01
	 * @param token
	 * @return
	 */
	public  String myFootMarkNum(String token){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		Integer num=footMarkDao.getCount(user.getId());
		ModelMap model=new ModelMap();
		model.put("num",num);
		return CodeContent.getModelByCode(1,"",model);
	}
}